<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input style="margin-bottom:10px"
                  type="text"
                  prefix-icon="el-icon-search"
                  size="small"
                  placeholder="输入员工姓名全员搜索"
                  v-model="queryParams.keyword"
                  @input="changeValue" />
        <!-- 树形组件 -->
        <el-tree node-key="id"
                 :data="depts"
                 :props="defaultProps"
                 default-expand-all
                 :expand-on-click-node="false"
                 highlight-current
                 ref="deptTree"
                 @current-change="currentChange" />
      </div>
      <div class="right">
        <el-row class="opeate-tools"
                type="flex"
                justify="end">
          <el-button size="mini"
                     type="primary"
                     @click="$router.push('employee/detail')">添加员工</el-button>
          <el-button size="mini"
                     @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini"
                     @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="user">
          <el-table-column label="头像"
                           prop="staffPhoto"
                           align="center">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto"
                         :src="row.staffPhoto"></el-avatar>
              <el-avatar v-else>{{ row.username?.charAt(2) }}</el-avatar>
            </template>
          </el-table-column>
          <el-table-column label="姓名"
                           prop="username"
                           align="center"></el-table-column>
          <el-table-column label="手机号"
                           prop="mobile"
                           align="center"></el-table-column>
          <el-table-column label="工号"
                           prop="workNumber"></el-table-column>
          <el-table-column label="聘用形式"
                           prop="formOfEmployment"
                           align="center">
            <template v-slot="{row}">
              <span>{{ row.formOfEmployment ===1? '正式':'非正式' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门"
                           prop="departmentName"
                           align="center"></el-table-column>
          <el-table-column label="入职时间"
                           prop="timeOfEntry"
                           align="center"></el-table-column>
          <el-table-column label="操作"
                           align="center">
            <template v-slot="{row}">
              <el-row type="flex"
                      justify="center">
                <el-col>
                  <el-button type="text"
                             @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
                  <el-button type="text"
                             @click="RoleEdit(row.id)">角色</el-button>
                  <el-popconfirm title="确定删除这一条数据吗"
                                 @onConfirm="confirmDel(row.id)">

                    <el-button type="text"
                               slot="reference">删除</el-button>
                  </el-popconfirm>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row align="middle"
                type="flex"
                justify="end">
          <el-pagination layout="total, prev, pager, next"
                         :total="user.total"
                         :current-page="queryParams.page"
                         :page-size="queryParams.pagesize"
                         @current-change="changePage">
          </el-pagination>
        </el-row>

      </div>
    </div>
    <import-excel :show-excel-dialog.sync="showExcelDialog"
                  @oploadSuccess="getDepartment" />
    <!-- 分配角色弹层 -->
    <el-dialog :visible.sync="showRoleDialog"
               title="分配角色">
      <el-checkbox-group v-model="RoleIds">

        <el-checkbox v-for="item in RoleList"
                     :key="item.id"
                     :label="item.id">
          {{ item.name }}
        </el-checkbox>

      </el-checkbox-group>
      <el-row type="flex"
              justify="center">
        <el-button type="primary"
                   @click="confirmRole">确定</el-button>
        <el-button @click="showRoleDialog = false">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { transListToTree } from '@/utils'
import { getEmployee, exportEmployee, delEmployee, getRoleList, getEmployeeDetail, assignRoles } from '@/api/employee'
import importExcel from '@/views/employee/components/import_excel.vue'
import FileSaver from 'file-saver'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data () {
    return {
      depts: [],
      user: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      showExcelDialog: false,
      showRoleDialog: false,
      RoleList: [],
      RoleIds: [],
      currentRoleIds: null
    }
  },
  created () {
    this.getDepartment()

  },
  methods: {
    async getDepartment () {

      this.depts = transListToTree(await getDepartment(), 0)
      this.queryParams.departmentId = this.depts[0].id
      // console.log(this.queryParams.departmentId)
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getEmployee()

    },
    currentChange (node) {
      this.queryParams.departmentId = node.id
      this.getEmployee()
    },
    async getEmployee () {
      const { rows, total } = await getEmployee(this.queryParams)
      this.user = rows
      this.user.total = total
    },
    changePage (newPage) {
      this.queryParams.page = newPage
      this.getEmployee()
    },
    changeValue () {
      // console.log(this.queryParams.keyword);
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployee()
      }, 1000);

    },
    async exportEmployee () {

      const result = await exportEmployee();
      FileSaver.saveAs(result, '员工信息表.xlsx')
      // console.log(result);

    },
    async confirmDel (id) {
      await delEmployee(id)
      if (this.user.length === 1 && this.queryParams.page > 1) {
        this.queryParams.page--

      }
      this.getDepartment()
      this.$message.success('删除成功')
    },
    async RoleEdit (id) {
      this.RoleList = await getRoleList()
      this.showRoleDialog = true
      this.currentRoleIds = id
      const { roleIds } = await getEmployeeDetail(id)
      this.RoleIds = roleIds
    },
    async confirmRole () {
      await assignRoles({
        id: this.currentRoleIds,
        roleIds: this.RoleIds
      })
      this.showRoleDialog = false
      this.$message.success('分配成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>